class PromiseProxy {
    constructor() {
        this.linteners = new Map();
        this.promise = null;
    }

    createPromise() {
        this.promise = new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
        })
        return this.promise;
    }


    on(event, callback) {
        if (!this.linteners.has(event)) {
            this.linteners.set(event, []);

        }
        this.linteners.get(event).push(callback);
    }

    emit(event, data) {
        const callbacks = this.linteners.get(event) || [];
        callbacks.forEach(callback => callback(data))
    }


    //触发成功
    resolve(data) {
        this.emit('success', data);
        this.resolve(data);
    }


    //触发失败
    reject(data) {
        this.emit('err', data);
        this.reject(data);

    }

}


const promiseProxy = new PromiseProxy();
const promiseOnProxy = promiseProxy.createPromise();
promiseProxy.on('success', (data) => {
    console.log('success', data);
})
promiseProxy.on('error', (err) => {
    console.log('error', err);
})


promiseOnProxy.then(data => {
    console.log('promiseOnProxy--resolve', data);
}).catch(err => {
    console.log('promiseOnProxy--reject', err);
});


// promiseProxy.resolve('hello world');

promiseProxy.reject('hello world----err');